//
//  BGHomeCellModel.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/25.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit

//该模型提供cell里的内容
class BGHomeCellModel: NSObject {
    //数据模型
    var statusModel: BGStatusModel?
    
    // 微博认证用户图片
    var avatarImage: UIImage?
    //处理会员等级图片
    var memberImage: UIImage?
    
    // 微博来源处理富文本
    var sourceAttr: NSMutableAttributedString?
    
    // 转发数
    var repostsCountStr: String?
    // 评论数
    var commentsCountStr: String?
    // 表态数
    var attitudesCountStr: String?
    
    //时间
    var createdTime: String?
    
    //重载构造函数
    init(statusModel: BGStatusModel) {
        super.init()
        self.statusModel = statusModel
        
        //处理用户图片和会员等级图片
        self.avatarImage = self.getAvatarImage(statusModel.user?.verified)
        self.memberImage = self.getMemberImage(statusModel.user?.mbrank)
        self.repostsCountStr = self.getSinaCount(statusModel.reposts_count, tilte: "转发")
        self.commentsCountStr = self.getSinaCount(statusModel.comments_count, tilte: "评论")
        self.attitudesCountStr = self.getSinaCount(statusModel.attitudes_count, tilte: "赞")
        //来源
        self.sourceAttr = self.getSourceStr(statusModel.source)
        //时间
        self.createdTime = NSDate.sinaDate(statusModel.created_at!)?.sinaDescripitionStr
        
    }
    
}

//MARK: - 文字处理 
extension BGHomeCellModel {
    private func getSinaCount(count: Int, tilte: String) -> String{
        // 如果count <= 0
        if count <= 0 {
            return tilte
        }else {
            // count > 0
            
            // 小于10000
            if count < 10000 {
                return "\(count)"
            }else {
                let sinaCount = CGFloat(count/1000)/10
                //  字符串拼接
                var sinaCountStr = "\(sinaCount)万"
                // 如果字符串包含.0
                if sinaCountStr.containsString(".0") {
                    // 字符串替换
                    sinaCountStr = sinaCountStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return sinaCountStr
            }
        }
        
    }
    
    
    func getSourceStr(source: String?)-> NSMutableAttributedString{
        // 判断他是否为nil 而且是否保存">
        guard let s = source where s.containsString("\">") else{
            let str = "上海三期微博"
            return getSourceAttr(str)
        }
        // 起始位置和结束位置
        let startRange = s.rangeOfString("\">")!
        let endRange = s.rangeOfString("</")!
        
        
        // 字符串截取
        let result = s.substringWithRange(startRange.endIndex..<endRange.startIndex)
        return getSourceAttr(result)
    }
    // 处理显示微博来源的富文本
    func getSourceAttr(result: String) -> NSMutableAttributedString {
        // 最终显示格式
        let name = "来自 " + result
        // 得到range
        let range = (name as NSString).rangeOfString("来自")
        
        // 创建一个可变富文本
        let attr = NSMutableAttributedString(string: name)
        // 设置属性
        attr.addAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], range: range)
        
        return attr
    }


}

//MARK: - 图片处理
extension BGHomeCellModel {
    
     // 处理微博认证用户图片/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    private func getAvatarImage(verified: Int?) -> UIImage? {
        //判断是否为nil
        guard let ver = verified else {
            return nil
        }
        
        switch ver {
        case 1:
            //1,认证用户
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            //2,3,5: 企业认证
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            //220: 达人
            return UIImage(named: "avatar_grassroot")
        case -1:
            //-1：没有认证
            return nil
        default:
            //随便给个妹子认证
            return UIImage(named: "avatar_vgirl")
        }
    }
    
    // 处理会员等级图片/// 会员等级 1-6
    private func getMemberImage(memberRank: Int?) -> UIImage? {
        
        guard let rank = memberRank else {
            return nil
        }
        
        if rank>0 && rank<7 {
            return UIImage(named: "common_icon_membership_level\(rank)")
        }else {
            return nil
        }
    }
    
}
